﻿using TianYuan.Utils.Logging;
using System;
using StackExchange.Redis;
using Newtonsoft.Json.Linq;
using TianYuan.Utils.Extensions;
using System.Net;

namespace TianYuan.Logging.Redis
{
    /// <summary>
    /// log4net 日志输出者适配类
    /// </summary>
    internal class RedisMqtLog : LogBase
    {
        private static readonly Type DeclaringType = typeof(RedisMqtLog);
        private readonly string _redislogkey = "";
        private readonly IDatabase _db;
        /// <summary>
        /// 初始化一个<see cref="RedisMqtLog"/>类型的新实例
        /// </summary>
        public RedisMqtLog(IDatabase db, string redislogkey)
        {
            _db = db;
            _redislogkey = redislogkey;
        }

        #region Overrides of LogBase

        /// <summary>
        /// 获取日志输出处理委托实例
        /// </summary>
        /// <param name="level">日志输出级别</param>
        /// <param name="message">日志消息</param>
        /// <param name="exception">日志异常</param>
        /// <param name="isData">是否数据日志</param>
        protected override void Write(LogLevel level, object message, Exception exception, bool isData = false)
        {
            if (isData)
            {
                return;
            }

            String logevel = level.ToString().ToUpper();
            if (message.GetType() != typeof(string))
            {
                //message = message.ToJsonString();
                message = message.ToJsonString();
            }
            
            JObject jObject = new JObject();
            jObject.Add(new JProperty("hostname", Dns.GetHostName()));
            jObject.Add(new JProperty("level", logevel));
            jObject.Add(new JProperty("datetime", DateTime.Now.ToMillisecondString()));
            jObject.Add(new JProperty("message", message));

            _db.ListLeftPush(_redislogkey, jObject.ToJsonString());
        }


        /// <summary>
        /// 获取 是否数据日志对象
        /// </summary>
        public override bool IsDataLogging
        {
            get { return false; }
        }

        /// <summary>
        /// 获取 是否允许输出<see cref="System.LogLevel.Trace"/>级别的日志
        /// </summary>
        public override bool IsTraceEnabled
        {
            get { return true; }
        }

        /// <summary>
        /// 获取 是否允许输出<see cref="System.Debug志
        /// </summary>
        public override bool IsDebugEnabled
        {
            get { return true; }
        }

        /// <summary>
        /// 获取 是否允许输出<see cref="System.LogLevel.Info"/>级别的日志
        /// </summary>
        public override bool IsInfoEnabled
        {
            get { return true; }
        }

        /// <summary>
        /// 获取 是否允许输出<see cref="System.LogLevel.Warn"/>级别的日志
        /// </summary>
        public override bool IsWarnEnabled
        {
            get { return true; }
        }

        /// <summary>
        /// 获取 是否允许输出<see cref="System.LogLevel.Error"/>级别的日志
        /// </summary>
        public override bool IsErrorEnabled
        {
            get { return true; }
        }

        /// <summary>
        /// 获取 是否允许输出<see cref="System.LogLevel.Fatal"/>级别的日志
        /// </summary>
        public override bool IsFatalEnabled
        {
            get { return true; }
        }

        #endregion Overrides of LogBase
    }
}